package org.wattdepot.cli.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.junit.Test;

/**
 * Tests the energy stats command.
 * 
 * @author George Lee
 *
 */
public class TestEnergyStatsCommand extends CommandTestBed {

  /**
   * Tests the do command method of the energy stats command.
   */
  @Override
  @Test
  public void testDoCommand() {
    String badParameter = "foo";
    //Sad tests
    //Correct invocations
    //energystats generated SIM_OAHU_GRID from 2009-11-01 to 2009-11-30 hourly
    //energystats generated SIM_OAHU_GRID from 2009-11-01 to 2009-11-30 daily
    EnergyStatsCommand energyStats = new EnergyStatsCommand(this.client);
    
    String command = "energystats";
    String result = energyStats.doCommand(command);
    assertFalse("Testing that command works.",
        result.contains(WattDepotCliCommand.UNKNOWN_COMMAND));
    assertTrue("Check that there are not enough parameters.",
        result.contains(WattDepotCliCommand.PARAMETER_COUNT_ERROR));
    
    command = "energystats foo SIM_OAHU_GRID from 2009-11-01 to 2009-11-30 hourly";
    result = energyStats.doCommand(command);
    assertFalse("Check that there are enough parameters.",
        result.contains(WattDepotCliCommand.PARAMETER_COUNT_ERROR));
    assertTrue("Check for invalid generated parameter.",
        result.contains(WattDepotCliCommand.PARAMETER_ERROR));
    
    command = "energystats generated SIM_OAHU_GRID foo 2009-11-01 to 2009-11-30 hourly";
    result = energyStats.doCommand(command);
    assertTrue("Check for invalid from parameter.",
        result.contains(WattDepotCliCommand.PARAMETER_ERROR + badParameter));
    
    command = "energystats generated SIM_OAHU_GRID from 2009-11-01 foo 2009-11-30 hourly";
    result = energyStats.doCommand(command);
    assertTrue("Check for invalid to parameter.",
        result.contains(WattDepotCliCommand.PARAMETER_ERROR + badParameter));
    
    command = "energystats generated SIM_OAHU_GRID from foo to 2009-11-30 hourly";
    result = energyStats.doCommand(command);
    assertTrue("Check for invalid start.",
        result.contains(WattDepotCliCommand.TSTAMP_ERROR + badParameter));
    
    command = "energystats generated SIM_OAHU_GRID from 2009-11-01 to foo hourly";
    result = energyStats.doCommand(command);
    assertTrue("Check for invalid end.",
        result.contains(WattDepotCliCommand.TSTAMP_ERROR + badParameter));
    
    command = "energystats generated SIM_OAHU_GRID from 2009-11-01 to 2009-11-30 foo";
    result = energyStats.doCommand(command);
    assertTrue("Check for invalid type parameter.",
        result.contains(WattDepotCliCommand.PARAMETER_ERROR + badParameter));
  }

  /**
   * Tests the retrieval of daily information using the energy stats command.
   */
  @Test
  public void testDailyCommand() {
    EnergyStatsCommand energyStats = new EnergyStatsCommand(this.client);
    String command = "energystats generated SIM_OAHU_GRID from 2009-11-01"
      + " to 2009-11-05 daily";
    String result = energyStats.doCommand(command);
    StringTokenizer tokenizer = new StringTokenizer(result);
    List<String> tokens = new ArrayList<String>();
    while (tokenizer.hasMoreTokens()) {
      tokens.add(tokenizer.nextToken());
    }
    assertEquals("Check that we got a valid result", tokens.get(0), "Max:");
    double value = Double.valueOf(tokens.get(1));
    assertTrue("Testing that we got a valid value.", value >= 0.0);
  }
  
  /**
   * Tests the retrieval of hourly information using the energy stats command.
   */
  @Test
  public void testHourlyCommand() {
    EnergyStatsCommand energyStats = new EnergyStatsCommand(this.client);
    
    String command = "energystats generated SIM_OAHU_GRID from 2009-11-01" 
      + " to 2009-11-02 hourly";
    String result = energyStats.doCommand(command);
    StringTokenizer tokenizer = new StringTokenizer(result);
    List<String> tokens = new ArrayList<String>();
    while (tokenizer.hasMoreTokens()) {
      tokens.add(tokenizer.nextToken());
    }
    
    assertEquals("Check that we got a valid result", tokens.get(0), "Max:");
    double value = Double.valueOf(tokens.get(1));
    assertTrue("Testing that we got a valid value.", value >= 0.0);
  }
}
